var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "@turf/turf", "../ProgressBar/ProgressBar", "../arrChunk"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const turf = __importStar(require("@turf/turf"));
    const ProgressBar_1 = __importDefault(require("../ProgressBar/ProgressBar"));
    const arrChunk_1 = __importDefault(require("../arrChunk"));
    const pg = new ProgressBar_1.default('聚合多边形', 40);
    let featuresLength = 0;
    let completedIndex = 0;
    function union(sourceGeoJson) {
        let resGeoJson;
        turf.featureEach(sourceGeoJson, function (currentFeature, featureIndex) {
            if (featureIndex === 0) {
                resGeoJson = currentFeature;
            }
            else {
                resGeoJson = turf.union(resGeoJson, currentFeature);
            }
            completedIndex++;
            pg.render({
                completed: completedIndex,
                total: featuresLength,
                description: '聚合多边形'
            });
        });
        return resGeoJson;
    }
    function unionGeoJson(geoJson) {
        const sourceGeoJson = turf.flatten(geoJson);
        featuresLength = sourceGeoJson.features.length;
        completedIndex = 0;
        const group = [];
        if (featuresLength > 200) {
            const chunkSize = Math.ceil(featuresLength / 10);
            const chunk = (0, arrChunk_1.default)(sourceGeoJson.features, chunkSize);
            for (let i = 0; i < chunk.length; i++) {
                group.push(chunk[i]);
            }
        }
        else {
            group.push(sourceGeoJson.features);
        }
        const groupGeoJson = [];
        for (let i = 0; i < group.length; i++) {
            const collection = turf.featureCollection(group[i]);
            groupGeoJson.push(union(collection));
        }
        let sumGeoJson = union(turf.featureCollection(groupGeoJson));
        return turf.flatten(sumGeoJson);
    }
    exports.default = unionGeoJson;
});
